//
//  GXSuggestArticelTableViewCell.m
//  GXLanOuFirstProgram
//
//  Created by 甘霞 on 15/10/31.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "GXSuggestArticelTableViewCell.h"
#import "GXCircleImageViewAndLabel.h"
#import "NSString+GXSizeWithText.h"
#import "BoaderManager.h"
#import "GXSuggestModel.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"

#define kContentViewWidth self.contentView.frame.size.width
#define kContentViewHeight self.contentView.frame.size.height

@interface GXSuggestArticelTableViewCell ()

//主要图片
@property (nonatomic, retain) UIImageView *mainImageView;
//显示图片数量
@property (nonatomic, retain) GXCircleImageViewAndLabel *ilView;
//标记图片
@property (nonatomic, retain) UIImageView *markImageView;
//标题
@property (nonatomic, retain) UILabel *titleLabel;
//简介
@property (nonatomic, retain) UILabel *introLabel;
//作者头像
@property (nonatomic, retain) UIImageView *authorImageView;
//作者名字
@property (nonatomic, retain) UILabel *authorLabel;
//显示时间
@property (nonatomic, retain) UILabel *showTimeLabel;

@end

@implementation GXSuggestArticelTableViewCell

#pragma mark - 初始化控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

#pragma mark - 创建子控件
- (void)createSubviews {
    
    //获取控制颜色的单例
    BOOL isNight = [[NSUserDefaults standardUserDefaults] boolForKey:@"isNight"];

    self.backgroundColor = isNight ? [UIColor blackColor] : [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeModel) name:@"changeModel" object:nil];
    
    //大图
    self.mainImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.mainImageView];
    [self.mainImageView setContentMode:UIViewContentModeScaleAspectFill];
    self.mainImageView.layer.masksToBounds = YES;
    
    //标记图
    self.markImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.markImageView];
    
    //标题
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel setFont:[UIFont fontWithName:kSuggestTitleFont size:kSuggestTitleFontSize]];
    self.titleLabel.textColor = isNight ? kNightColor_SuggestTitle : kDayColor_SuggestTitle;
    [self.titleLabel setNumberOfLines:0];
    [self.titleLabel sizeToFit];
    
    //简介
    self.introLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.introLabel];
    self.introLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
    [self.introLabel setFont:[UIFont fontWithName:kSuggestIntroFont size:KSuggestIntroFontSize]];
    [self.introLabel setNumberOfLines:0];
    [self.introLabel sizeToFit];
    
    //作者名字
    self.authorLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.authorLabel];
    [self.authorLabel setFont:[UIFont fontWithName:kSuggestAuthorFont size:kSuggestAuthorFontSize]];
    self.authorLabel.textColor = isNight ? kNightColor_SuggestAuthor : kDayColor_SuggestAuthor;
    
    //作者头像
    self.authorImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.authorImageView];
    self.authorImageView.layer.cornerRadius = 15;
    self.authorImageView.layer.masksToBounds = YES;
    
    //显示图片数量
    self.ilView = [[GXCircleImageViewAndLabel alloc] init];
    [self.mainImageView addSubview:self.ilView];
    
    //音乐时间标签
    self.showTimeLabel = [[UILabel alloc] init];
    self.showTimeLabel.textColor = isNight ? kNightColor_SuggestShowTime : kDayColor_SuggestShowTime;
    [self.showTimeLabel setFont:[UIFont fontWithName:kSuggestMediaFont size:kSuggestMediaFontSize]];
    [self.mainImageView addSubview:self.showTimeLabel];
    
    self.ilView.hidden = YES;
    self.showTimeLabel.hidden = YES;
}

- (void)changeModel {    
    BOOL isNight = [[NSUserDefaults standardUserDefaults]  boolForKey:@"changeModel"];
    self.backgroundColor = isNight ? [UIColor blackColor] : [UIColor whiteColor];
    self.titleLabel.textColor = isNight ? kNightColor_SuggestTitle : kDayColor_SuggestTitle;
    self.introLabel.textColor = isNight ? kNightColor_SuggestIntro : kDayColor_SuggestIntro;
    self.showTimeLabel.textColor = isNight ? kNightColor_SuggestShowTime : kDayColor_SuggestShowTime;
}

#pragma mark - 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    //大图
    [self.mainImageView setFrame:CGRectMake(kSideBorder, kSideBorder, kContentViewWidth - 2 * kSideBorder, kMainImageViewHeight)];
    
    //标记图片
    CGFloat markImageViewY = CGRectGetMaxY(self.mainImageView.frame) + kTitleLableTopBorder;
    CGSize markImageSize = self.markImageView.image.size;
    [self.markImageView setFrame:CGRectMake(kSideBorder, markImageViewY, markImageSize.width, markImageSize.height)];
    
    //标题
    CGFloat titleLabelX = CGRectGetMaxX(self.markImageView.frame) + kTitleLabelLeftBorder;
    CGFloat titleLabelW = kContentViewWidth - kSideBorder - titleLabelX;
    CGRect titleLabelSize = [self.titleLabel.text SizeWithTextFontSize:kSuggestTitleFontSize textWidth:titleLabelW];
    [self.titleLabel setFrame:CGRectMake(titleLabelX, self.markImageView.frame.origin.y, titleLabelW, titleLabelSize.size.height)];
    
    //副标题
    CGFloat introLabelY = CGRectGetMaxY(self.titleLabel.frame) + kSubTitleLabelTopBorder;
    CGRect introLabelSize = [self.introLabel.text SizeWithTextFontSize:15 textWidth:titleLabelW];
    [self.introLabel setFrame:CGRectMake(titleLabelX, introLabelY, titleLabelW, introLabelSize.size.height)];
    
    //作者头像
    CGFloat authorY = CGRectGetMaxY(self.introLabel.frame) + kAuthorTopLabelBorder;
    [self.authorImageView setFrame:CGRectMake(titleLabelX, authorY, 30, kAuthorImageViewSize)];
    
    //作者名字
    CGFloat authorLabelX = CGRectGetMaxX(self.authorImageView.frame) + kAuthorNameLeftBorder;
    [self.authorLabel setFrame:CGRectMake(authorLabelX, authorY + 5, titleLabelW - kAuthorImageViewSize - kAuthorNameLeftBorder, kAuthorLabelHeight)];
    
    //显示图片数量
    CGSize ilViewSize = self.ilView.imageView.image.size;
    [self.ilView setFrame:CGRectMake(self.mainImageView.frame.size.width - ilViewSize.width - 2 * kSideBorder, kSideBorder, ilViewSize.width * 3 / 2, ilViewSize.height * 3 / 2)];
    
    //音乐时间
    [self.showTimeLabel setFrame:CGRectMake(0, CGRectGetMaxY(self.mainImageView.frame) - 50, kContentViewWidth - 2 * kSideBorder, 30)];
}

- (void)setSuggestModel:(GXSuggestModel *)suggestModel {
    if (_suggestModel != suggestModel) {
        _suggestModel = suggestModel;
    }
    
    //文章
    if (_suggestModel.type.integerValue == 0) {
        //标记图片
        self.ilView.hidden = YES;
        self.markImageView.image = [UIImage imageNamed:@"icon-recommended"];
        if (_suggestModel.has_video == true) {
        }
    }
    //视频
    else if (_suggestModel.type.integerValue == 2) {
    }
    
    //音乐
    else if (_suggestModel.type.integerValue == 10) {
        self.showTimeLabel.hidden = NO;
        
        NSNumber *musicTime = _suggestModel.music[@"music_duration"];
        self.showTimeLabel.text = [NSString stringWithFormat:@"%02ld %02ld", musicTime.integerValue / 3, musicTime.integerValue % 3];
        
        //标记图片
        self.markImageView.image = [UIImage imageNamed:@"icon-recommended"];
    }
    
    //大型画册/九宫格
    else if (_suggestModel.type.integerValue == 9 || _suggestModel.type.integerValue == 1) {
        self.ilView.hidden = NO;
        self.ilView.label.text = [NSString stringWithFormat:@"%@张", [_suggestModel.gallery[@"image_count"] stringValue] ];
        self.markImageView.image = [UIImage imageNamed:@"icon-image-album"];
    }
    
    //图片
    [self.mainImageView sd_setImageWithURL:_suggestModel.thumb[@"raw"] placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
    //标题
    self.titleLabel.text = _suggestModel.title;
    //副标题
    self.introLabel.text = _suggestModel.content[@"desc"];
    //作者头像
    [self.authorImageView sd_setImageWithURL:_suggestModel.cp[@"avatar"]];
    //作者名字
    self.authorLabel.text = _suggestModel.cp[@"screen_name"];
}

- (void)setCoreSugModel:(SuggestModel *)coreSugModel
{
    if (_coreSugModel != coreSugModel) {
        _coreSugModel = coreSugModel;
    }
    
    //文章
    if (_coreSugModel.type.integerValue == 0) {
        //标记图片
        self.ilView.hidden = YES;
        self.markImageView.image = [UIImage imageNamed:@"icon-recommended"];
        
    }
    //视频
    else if (_coreSugModel.type.integerValue == 2) {
    }
    
    //音乐
    else if (_coreSugModel.type.integerValue == 10) {
        self.showTimeLabel.hidden = NO;
        
        NSNumber *musicTime = coreSugModel.has_video;
        self.showTimeLabel.text = [NSString stringWithFormat:@"%02ld %02ld", musicTime.integerValue / 3, musicTime.integerValue % 3];
        
        //标记图片
        self.markImageView.image = [UIImage imageNamed:@"icon-recommended"];
    }
    
    //大型画册/九宫格
    else if (coreSugModel.type.integerValue == 9 || coreSugModel.type.integerValue == 1) {
        self.ilView.hidden = NO;
        self.ilView.label.text = [NSString stringWithFormat:@"%@张", [coreSugModel.comment_count stringValue] ];
        self.markImageView.image = [UIImage imageNamed:@"icon-image-album"];
    }
    
    //图片
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:coreSugModel.share_image] placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
    //标题
    self.titleLabel.text = coreSugModel.title;
    //副标题
    self.introLabel.text = coreSugModel.type_name;
    //作者头像
    [self.authorImageView sd_setImageWithURL:[NSURL URLWithString:coreSugModel.share_text]];
    //作者名字
    self.authorLabel.text = coreSugModel.intro;
}

@end
